import storage from 'store'
import { login, logout } from '@/api/system/loginManage'
import { getInfo } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'
// getInfo, logout
const user = {
  state: {
    token: '',
    realname: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    buttons: [], // 按钮权限
    admintype: '' // 是否是 SuperAdmin
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_REALNAME: (state, { realname, welcome }) => {
      state.realname = realname
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    // Login ({ commit }, userInfo) {
    //   return new Promise((resolve, reject) => {
    //     login(userInfo).then(response => {
    //       const result = response.result
    //       storage.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
    //       commit('SET_TOKEN', result.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },
    Login({ commit }, loginParam) {
      return new Promise((resolve, reject) => {
        login(loginParam)
          .then((response) => {
            console.log('res', response)
            if (!response.success) {
              reject(response.message)
              return
            }
            const { userInfo, token } = response.result
            storage.set(ACCESS_TOKEN, token, 7 * 24 * 60 * 60 * 1000)
            // this.setUserInfo(userInfoToV2);
            // const { username, apps, menus } = userInfo
            commit('SET_TOKEN', token)
            commit('SET_INFO', userInfo)
            resolve()
            // eslint-disable-next-line handle-callback-err
          })
          .catch((error) => {
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('后端未启动或代理错误')
          })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then((response) => {
            const result = response.result

            if (result.role && result.role.permissions.length > 0) {
              const role = result.role
              role.permissions = result.role.permissions
              role.permissions.map((per) => {
                if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
                  const action = per.actionEntitySet.map((action) => {
                    return action.action
                  })
                  per.actionList = action
                }
              })
              role.permissionList = role.permissions.map((permission) => {
                return permission.permissionId
              })
              commit('SET_ROLES', result.role)
              commit('SET_INFO', result)
            } else {
              reject(new Error('getInfo: roles must be a non-null array !'))
            }

            commit('SET_REALNAME', { name: result.name, welcome: welcome() })
            commit('SET_AVATAR', result.avatar)

            resolve(response)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token)
          .then(() => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            storage.remove(ACCESS_TOKEN)
            resolve()
          })
          .catch(() => {
            resolve()
          })
          .finally(() => {})
      })
    }
  }
}

export default user
